package com.zhxy.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.AssociationDao;
import com.zhxy.manager.dto.AssociationTjDto;
import com.zhxy.manager.entity.Association;
import com.zhxy.manager.excel.AssociationExcel;
import com.zhxy.manager.service.intf.AssociationService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 兴趣社团
 *
 * @author ZM
 */
@Service
public class AssociationServiceImpl implements AssociationService {
    @Autowired
    private AssociationDao dao;

    //新增
    @Override
    public R save(Association association) {
        if (dao.add(association) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    //查询
    @Override
    public PageBean queryPage(int page, int limit) {
        //分页
        PageHelper.startPage(page,limit);
        //获取分页信息
        PageInfo<Association> pageInfo = new PageInfo<>(dao.selectAll());
        //返回
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }

    //查询全部返回list集合 到处表格
    @Override
    public List<Association> queryAll() {
        return dao.selectAll();
    }

    //根据id 删除
    @Override
    public R del(int id) {
        if (id > 0) {
            if (dao.del(id) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        } else {
            return R.fail();
        }
    }

    //修改
    @Override
    public R update(Association association) {
        if (dao.update(association) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }
    //条件查询
    @Override
    public PageBean queryPageWhere(String name, int page, int limit) {
        //验证查询条以  如果为 " "  那么赋值null
        if (!StringUtils.hasLength(name)) {
            name = null;
        } else {
            name = "%"+ name +"%"; //拼接上模糊查询
        }
        //分页
        PageHelper.startPage(page, limit);
        //获取分页结果结果
        PageInfo<Association> pageInfo = new PageInfo<>(dao.selectWhere(name));
        //返回分页数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    //批量删除
    @Override
    public R delBatch(int[] ids) {
        if (ids != null && ids.length > 0) {
            //执行
            if (dao.delBatch(ids) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    //批量导入
    @Override
    public R saveBatch(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            //excel解析
            List<AssociationExcel> list= EasyExcel.read(file.getInputStream(), AssociationExcel.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {}
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
            }).sheet().doReadSync();
            System.out.println(list);
            //执行dao 批量新增
            if(dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    //统计图
    @Override
    public R queryTj() {
        //1.查询统计数据
        List<AssociationTjDto> list=dao.selectTj();
        System.out.println(list);
        //2.根据前端 组装数据
        Map<String,List<Object>> map=new HashMap<>();
        map.put("name",new ArrayList<>());//社团名称
        map.put("persons",new ArrayList<>());//人数
        //3.组装数据  函数式编程
        list.forEach(f->{
            map.get("name").add(f.getName());
            map.get("persons").add(f.getNums());
        });
        return R.ok(map);
    }

}
